define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'service/money/withdraw/index' + location.search,
                    multi_url: 'service/money/withdraw/multi',
                    import_url: 'service/money/withdraw/import',
                    table: 'service_withdraw',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'info.name', title: __('Name'),operate: false},
                        {field: 'info.mobile', title: __('Mobile'),operate: false},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3'),"4":__('Type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'withdrawtype', title: __('Withdrawtype'), searchList: {"0":__('Withdrawtype 0'),"1":__('Withdrawtype 1')}, formatter: Table.api.formatter.normal},
                        {field: 'withdraw_info', title: __('Withdraw_info'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'num', title: __('Num'), operate:'BETWEEN'},
                        {field: 'images', title: __('Images'), operate:false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'state', title: __('State'), searchList: {"-1":__('State -1'),"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        {field: 'note', title: __('Note'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name:'audit',
                                    text:'通过',
                                    title:'通过',
                                    icon: 'fa fa-check',
                                    classname: 'btn btn-xs btn-success  btn-ajax',
                                    url: 'service/money/withdraw/agree',
                                    confirm: '确认通过？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger('click');
                                    },

                                    hidden:function(row){
                                        if(row.state != 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name:'audit',
                                    text:'已转账',
                                    title:'已转账',
                                    icon: 'fa fa-check',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url: function(row){
                                        return 'service/money/withdraw/payment?ids='+row.id;
                                    },
                                    confirm: '确认已转账？',
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger('click');
                                    },

                                    hidden:function(row){
                                        if(row.state != 1){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'refuse',
                                    text: __('拒绝'),
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-close',
                                    url: function(row){
                                        return 'service/money/withdraw/refuse?ids='+row.id;
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger('click');
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    callback: function (data) {
                                       $(".btn-refresh").trigger('click');
                                    },
                                    hidden:function(row){
                                        if(row.state != 0){
                                            return true;
                                        }
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            
        },
        payment: function () {
            $(document).on('click', '.btn-embossed', function () {
                parent.$(".btn-refresh").trigger('click');
            });
            Controller.api.bindevent();
        },
        refuse: function () {
            
            $(document).on('click', '.btn-embossed', function () {
                parent.$(".btn-refresh").trigger('click');
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
